package com.fj.netty.chat.handler;

import com.fj.netty.chat.message.GroupMembersRequestMessage;
import com.fj.netty.chat.message.GroupMembersResponseMessage;
import com.fj.netty.chat.server.service.ServiceFactory;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.Set;

/**
 * @author fj
 * @date 2024/1/12 16:24
 */
@Slf4j
public class GroupMembersRequestMessageHandler extends SimpleChannelInboundHandler<GroupMembersRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupMembersRequestMessage msg) throws Exception {
        Set<String> members = ServiceFactory.getGroupSession().members(msg.getGroupName());
        GroupMembersResponseMessage groupMembersResponseMessage = new GroupMembersResponseMessage();
        groupMembersResponseMessage.setMembers(members);
        ctx.writeAndFlush(groupMembersResponseMessage);
    }
}
